﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

namespace WeakEndForum
{
    public partial class EditNews : System.Web.UI.Page
    {
        int aID = 0;
        string getURIParam = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Modules.isRequestedQueryStringValid("aID", "ID", "Actualite", out getURIParam))
            {
                aID = Convert.ToInt32(getURIParam);
            }
            else
            {
                Server.Transfer("Error404.aspx");
            }
            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand isModValid = new OleDbCommand("Select Auteur FROM Actualite WHERE ID = @aID ;", conn);
            isModValid.Parameters.Add(new OleDbParameter("aID", aID) { OleDbType = OleDbType.Integer });
            isModValid.Prepare();
            OleDbDataReader dataReader = isModValid.ExecuteReader();
            if (dataReader.Read())
            {
                if (!Modules.isMod(Modules.getConnectedUser()) && Modules.getConnectedUser() != (string)dataReader[0])
                {
                    Server.Transfer("Error403.aspx");
                }
            }
            conn.Close();
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            {
                aID = Convert.ToInt32(getURIParam);
                OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
                conn.Open();
                OleDbCommand getMessage = new OleDbCommand("SELECT Contenu FROM Actualite WHERE ID = @aID ;", conn);
                getMessage.Parameters.Add(new OleDbParameter("aID", aID) { OleDbType = OleDbType.Integer });
                getMessage.Prepare();
                OleDbDataReader dataReader = getMessage.ExecuteReader();
                if (dataReader.Read())
                {
                    txtEditNewsContent.Text = (string)dataReader[0];
                }
                conn.Close();
            }
        }

        protected void btnEditNews_Click(object sender, EventArgs e)
        {
            string updatedMessage = txtEditNewsContent.Text;
            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand EditMessage = new OleDbCommand("UPDATE Actualite SET Contenu = @Contenu WHERE ID = @ID;", conn);
            EditMessage.Parameters.Add(new OleDbParameter("Contenu", updatedMessage) { OleDbType = OleDbType.LongVarChar, Size = 65535 });
            EditMessage.Parameters.Add(new OleDbParameter("ID", aID) { OleDbType = OleDbType.Integer });
            EditMessage.Prepare();
            EditMessage.ExecuteNonQuery();
            conn.Close();
            string redirectURL = "Article.aspx?aID=";
            redirectURL += aID.ToString();
            Response.Redirect(redirectURL);

        }
    }
}